/**
 * 
 */
package net.cmath.aop;

import javax.servlet.http.Cookie;

import net.cmath.auth.AuthContext;
import net.cmath.auth.AuthUser;
import net.cmath.po.FlexObject;

import org.aspectj.lang.ProceedingJoinPoint;

import flex.messaging.FlexContext;

/**
 * @author Administrator
 *
 */
public class AuthAop {
	public Object doAuth(ProceedingJoinPoint pjp) throws Throwable {
		Cookie[] cookies = (Cookie[]) FlexContext.getHttpRequest().getCookies();
		String AI = null;
		for(Cookie cookie : cookies) {
			if ("AI".equals(cookie.getName())) {
				AI = cookie.getValue();
			}
		}
		FlexObject retVal = new FlexObject();
		AI = AuthContext.decode(AI);
		if (AI != null) {
			AuthUser user = (AuthUser)FlexContext.getFlexSession().getAttribute("authInfo");
			if (Integer.parseInt(AI) ==user.getUserId()) {
				retVal = (FlexObject)pjp.proceed();
			} else {
				retVal.setCode(403);
			}
		} else {
			retVal.setCode(403);
		}
		return retVal;
	}
}
